#include"SList.h"

void SListPrint(SLTNode* phead)
{
    SLTNode* cur = phead;
    while(cur)
    {
        printf("%d->",cur->data);
        cur = cur->next;
    }
    printf("NULL\n");
}
SLTNode* BuySListNode(SLTDataType x)
{

    SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
    assert(newnode);
    newnode->data = x;
    newnode->next = NULL;

    return newnode;
}
void SListPushBack(SLTNode** pphead, SLTDataType x)
{
    assert(pphead);
   SLTNode* newnode =  BuySListNode(x);
    //申请节点
  //  SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
  //  assert(newnode);
  //  newnode->data = x;
  //  newnode->next = NULL;
    //找尾
    //SLTNode* tail = phead;
    //while(tail->next != NULL)
    //{
    //    tail = tail->next;
    //}
    //
    if(*pphead == NULL)
    {
        *pphead = newnode;
    }
    else
    {

        SLTNode* tail = *pphead;
        while(tail->next != NULL)
         {
            tail = tail->next;
         }
         //链接
         tail->next = newnode;
    }


}
void SListPushFront(SLTNode** pphead, SLTDataType x)
{
    assert(pphead);
    SLTNode* newnode = BuySListNode(x);
    newnode->next = *pphead;
    *pphead = newnode;
}
void SListPopBack(SLTNode** pphead)
{
    assert(pphead);
    assert(*pphead);
  //  if(*pphend == NULL) return;
    if((*pphead)->next == NULL)
    {
        free(*pphead);
        *pphead = NULL;
    }
    else{

//    SLTNode* tail = *pphead;
//    while(tail->next->next != NULL)
//    {
//        tail = tail->next;
//    }
//    free(tail->next);
//    tail->next = NULL;
//    }
    SLTNode* tail = *pphead;
    SLTNode* tailprev = NULL;
    while(tail->next != NULL)
    {
        tailprev = tail;
        tail = tail->next;
    }
    free(tail);
    tailprev->next = NULL;
    }
}
void SListPopFront(SLTNode** pphead)
{
    assert(pphead);
    assert(*pphead);
    //if((*pphead) == NULL) return;
   // SLTNode* next =(*pphead)->next;
   // free(*pphead);
   // *pphead = next;
   SLTNode* head = *pphead;
   *pphead = (*pphead)->next;
   free(head);
}

SLTNode* SListFind(SLTNode* phead, SLTDataType x)
{
    SLTNode* cur = phead;
    while(cur->next)
    {
        if(cur->data == x)
        return cur;
        cur = cur->next;
    }
    return NULL;
}
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
    assert(pos);
    assert(pphead);
    if((*pphead) == pos)
    {
        SListPushFront(pphead, x);
    }
    else{
        SLTNode* prev = *pphead;
        while(prev->next != pos)
        {
            prev = prev->next;
        }
        SLTNode* newnode = BuySListNode(x);
        prev->next = newnode;
        newnode->next = pos;
    }

}
void SListErase(SLTNode** pphead, SLTNode* pos)
{
    assert(pphead);
    assert(pos);
    if(*pphead == pos)
    {
        SListPopFront(pphead);
    }
    else{
        SLTNode* prev = *pphead;
        while(prev->next != pos)
        {
            prev =prev->next;
        }
        prev->next = pos->next;
        free(pos);
        pos = NULL;
    }
}
void SListInsertAfter(SLTNode* pos, SLTDataType x)
{
    assert(pos);
    SLTNode* newnode = BuySListNode(x);
   // newnode->next = pos->next;
   // pos->next = newnode;
    SLTNode* next = pos->next;
    newnode->next = next;
    pos->next = newnode;
}
void SListInsertAfter(SLTNode* pos)
{
    assert(pos);
    if(pos->next == NULL)
    {
        return;
    }

    SLTNode* del= pos->next;
   // pos->next = pos->next->next;
    pos->next =del->next;
    free(del);
    del = NULL;
}
